
#保存文件
# write.csv(risk2016,'data/risk2016.csv',fileEncoding = 'UTF-8',row.names = FALSE)

library(maptools)
library(ggplot2) 
library(raster)
library(dplyr)
dir.create('output')

risk2016<-read.csv('data/risk2016.csv',header = TRUE,sep=',')%>%dplyr::select(-one_of('分级'))
china_map<-raster::getData('GADM',country='CHN',level=1)
china_map2<-fortify(china_map)
# china_map <- readShapePoly("/home/yangsj/mnt/中国省级行政区/province_2004.shp")
# china_map2 <- fortify(china_map)

brks<-c(-30,0,30,60,90)
brks.lab<-c(min(risk2016$风险系数),-30,0,30,60,90,max(risk2016$风险系数))
labs<-c(paste("<",first(brks),sep = ""),
        paste(brks[-length(brks)],brks[-1],sep = "~"),
        paste(">=",last(brks),sep = ""))

risk2016$分级<-cut(risk2016$风险系数,breaks=brks.lab,labels=labs,include.lowest=TRUE,dig.lab = 5)
risk2016$分级<-factor(risk2016$分级,levels = rev(labs))
risk2016$城市等级<-factor(risk2016$城市等级,levels = c('一级城市','二级城市','三级城市','四级城市'))

qd_plot<-ggplot() +
  geom_polygon(colour = 'grey70', fill = 'white',data = china_map2,aes(x = long, y = lat,  group = group),size=0.3)+
  geom_point(data=risk2016,mapping=aes(x=经度,y=纬度,colour=分级,size=城市等级))+
  theme_bw()+
  labs(title='2016年9月中国城市住房价格风险分布',x='',y='',colour='风险系数(%)')+
  scale_size_manual(values = c(4,3,2,1))+
  guides(size=FALSE)+theme(legend.position=c(0.9,0.22))+
  scale_colour_manual(values=c('red3','red','#FF9540','#C0F400','#6BE400','skyblue'))+
  coord_cartesian(ylim = c(18,53))+
  theme(axis.text=element_blank(),axis.ticks = element_blank())

# devtools::install_github("dgrtwo/gganimate")
# library(gganimate)
# gg_animate(qd_plot,filename = 'output/qd_plot.gif')
tiff(file = "output/qd_plot.tiff", width=2000,height=1500,compression='lzw+p',res=300)
print(qd_plot)
dev.off()
print(qd_plot)

